package com.test.springboot.springboot3testdemo3.service;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.CacheManager;
import com.alicp.jetcache.anno.CreateCache;
import com.test.springboot.springboot3testdemo3.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/9/14-09:28
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Service
public class JetCacheProgramUserService {
  private static final Logger LOGGER = LoggerFactory.getLogger(JetCacheProgramUserService.class);

  @CreateCache(expire = 100)
  private Cache<Integer, User> userCache;

  private final CacheManager cacheManager;

  public JetCacheProgramUserService(CacheManager cacheManager) {
    this.cacheManager = cacheManager;
  }

  /*@PostConstruct
  public void init() {
    QuickConfig qc =
        QuickConfig.newBuilder("user:")
            .expire(Duration.ofSeconds(100))
            .cacheType(CacheType.BOTH)
            .syncLocal(true)
            .build();
    userCache = cacheManager.getOrCreateCache(qc);
  }*/

  public User save(User user) {
    LOGGER.info("Program 保存用户信息");
    this.userCache.computeIfAbsent(user.getId(), key -> user);
    return user;
  }

  public User get(int userId) {
    User user = this.userCache.get(userId);
    if (user == null) {
      LOGGER.info("查询用户信息");
      // TODO
    }
    return user;
  }
}
